using System;
using System.IO;
using Unity.IO.LowLevel.Unsafe;
using UnityEngine;

public static class CopyManager
{
    //复制文件
    public static bool CopyFileToOtherFile(string path1,string path1Name,string path2,string path2Name)
    {
        //如果文件夹1不存在
        if (CheckFileExist(path1, path1Name)==false) return false;
        //检查文件是否存在
        if (!CheckFileExist(path2, path2Name))
        {
            //创建文件夹
            CreateFile(path2, path2Name);
        }
        //查看是否存在子文件
        if (FileExistData(path2, path2Name))
        {
            //因为所有数据都要进行覆盖，直接删文件夹效率更高
            //删除文件
            DeleteDirectory(path2, path2Name);
            //创建空文件夹
            CreateFile(path2, path2Name);
        }
        //开始覆盖
        bool succ = Copy(path1, path1Name, path2, path2Name);

        return succ;
    }
    //查看文件是否存在
    public static bool CheckFileExist(string path, string pathName)
    {
        //得到文件路径
        string fullPath = Path.Combine(path, pathName);

        Debug.Log(fullPath+" / "+Directory.Exists(fullPath));

        return Directory.Exists(fullPath);
    }
    //创建文件夹
    public static void CreateFile(string path,string pathName)
    {
        //拼接文件夹
        string fullPath = Path.Combine(path, pathName);
        //创建文件夹
        Directory.CreateDirectory(fullPath);
    }
    //查看文件是否有数据
    public static bool FileExistData(string path,string pathName)
    {
        //拼接路径
        string fullPath = Path.Combine(path,pathName);
        //存在才获取
        if (Directory.Exists(fullPath)==false) return false;
        //获取子文件
        string[] childrenFile = Directory.GetFiles(fullPath);

        //我这里只需要检查子文件，没有检查子文件夹，目前用不到，后续可以添加上

        return childrenFile.Length>0;
    }
    //删除文件夹
    public static void DeleteDirectory(string path,string pathName)
    {
        //拼接路径
        string fullPath = Path.Combine(path, pathName);
        //存在才删除
        if(Directory.Exists(fullPath))
        {
            //清空文件夹
            Directory.Delete(fullPath,true);
        }
    }
    //删除文件
    public static void DeleteFile(string fullPath)
    {
        //存在文件才删除
        if(File.Exists(fullPath))
        {
            File.Delete(fullPath);
        }
    }
    //开始复制
    public static bool Copy(string path1,string path1Name,string path2,string path2Name)
    {
        try
        {
            //获取路径
            string full1Path = Path.Combine(path1, path1Name);
            string full2Path = Path.Combine(path2, path2Name);

            //获取文件夹1所有数据
            string[] path1Files = Directory.GetFiles(full1Path);

            //将所有数据复制到文件夹2
            foreach (var i in path1Files)
            {
                //获取文件名称
                string fileName = Path.GetFileName(i);
                //拼接文件夹2的路径
                string fullPath = Path.Combine(full2Path, fileName);
                //复制，同名也覆盖
                File.Copy(i, fullPath, true);
            }

            return true;
        }
        catch(Exception e)
        {
            Debug.Log("复制出错："+e.ToString());

            return false;
        }
    }
}